﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.Text;
using BusinessObject;
using YamahaTuanTrucService;

namespace YamahaTuanTrucService
{
    // NOTE: You can use the "Rename" command on the "Refactor" menu to change the class name "AuthenticationService" in code, svc and config file together.
    public class AuthenticationService : IAuthenticationService
    {
        public bool Authentication(string username, string password)
        {
            return true;
        }

        public int CheckPasswd(string username, string passwd)
        {
            using (var context = new YamahaTuanTrucEntities())
            {
                int id = -1;
                Admin admin = context.Admin.SingleOrDefault(p => p.username == username && p.password == passwd);
                if (admin != null)
                {
                    id = admin.ID;
                }
                return id;
            }
        }

        public bool UpdatePasswd(int id, string newpass)
        {
            using (var context = new YamahaTuanTrucEntities())
            {
                Admin admin = context.Admin.SingleOrDefault(p=> p.ID == id);
                admin.password = newpass;
                try
                {
                    context.SaveChanges();
                    return true;
                }
                catch (Exception e)
                {
                    Console.WriteLine(e);
                    return false;
                }
            }
        }
    }
}
